package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateGoods
//@description: 创建Goods记录
//@param: goods model.Goods
//@return: err error

func CreateGoods(goods model.Goods) (err error) {
	err = global.GVA_DB.Create(&goods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteGoods
//@description: 删除Goods记录
//@param: goods model.Goods
//@return: err error

func DeleteGoods(goods model.Goods) (err error) {
	err = global.GVA_DB.Delete(goods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteGoodsByIds
//@description: 批量删除Goods记录
//@param: ids request.IdsReq
//@return: err error

func DeleteGoodsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Goods{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateGoods
//@description: 更新Goods记录
//@param: goods *model.Goods
//@return: err error

func UpdateGoods(goods model.Goods) (err error) {
	err = global.GVA_DB.Save(&goods).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetGoods
//@description: 根据id获取Goods记录
//@param: id uint
//@return: err error, goods model.Goods

func GetGoods(id uint) (err error, goods model.Goods) {
	err = global.GVA_DB.Where("id = ?", id).First(&goods).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetGoodsInfoList
//@description: 分页获取Goods记录
//@param: info request.GoodsSearch
//@return: err error, list interface{}, total int64

func GetGoodsInfoList(info request.GoodsSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Goods{})
    var goodss []model.Goods
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&goodss).Error
	return err, goodss, total
}